var express = require('express');
var router = express.Router();
const MyQuery = require('../utils/db')

const { v4: uuidv4 } = require('uuid');//引入uuid，为了生成一个唯一不重复的字符串
const md5 = require('md5')


//引入配置文件
const { API_URL,suozi,getToken } = require('../utils/config')

router.get('/demo4', function (req, res, next) {
    res.send('登录注册模块')
});

/*
    注册需要：
        id
        mid
        username
        password
        createdate
*/
router.post('/register', async (req, res, next) => {
    // console.log(1234)
    let { username = '', password = '', repwd = '' } = req.body //post请求，用req.body接收参数

    let data = {}//最终要返回的数据格式  msg,status,result

    if (username == '' || password == '' || repwd == '') {
        data = {
            msg: '请传递必要参数',
            status: 500
        }
        res.send(data)
        return
    }

    if (password != repwd) {
        data = {
            msg: '两次密码不一致',
            status: 500
        }
        res.send(data)
        return
    }

    let sql2 = `SELECT * FROM e_member WHERE username = '${username}'`
    let { result:result2 } = await MyQuery(sql2) //{ err,result }
    // res.send(result2)
    if( result2.length > 0 ){
        data = {
            "msg": "用户已被注册了",
            "status": 500 
        }
        res.send( data )
        return
    }
    // return

    let createdate = Date.now() //注册时间
    let mid = uuidv4()//用户的唯一id
    password = md5( password + suozi )
    //1. 写sql
    let sql = `INSERT INTO e_member ( mid,username,password,createdate ) VALUES ( '${mid}','${username}','${password}',${createdate} ) `
    //2. 执行sql
    let { result } = await MyQuery(sql) //{ err,result }

    //3. 要处理成规范、格式化的json数据
    data = {
        msg: 'OK',
        status: 200
    }
    res.send(data)
})

//登录
router.post('/login',async (req,res,next)=>{
    
    let {username = '', password = ''} = req.body
  
    let data = {}//最终要返回的数据格式  msg,status,result

    if (username == '' || password == '' ) {
        data = {
            msg: '请传递必要参数',
            status: 500
        }
        res.send(data)
        return
    }

    password = md5( password + suozi ) 
    //1. 写sql
    let sql = `SELECT * FROM e_member WHERE username = '${username}' AND password = '${password}'`

    // res.send( sql )
    // return
    //2. 执行sql
    let {result} = await MyQuery(sql) //{ err,result }
    if( result.length == 0 ){
        data = {
            msg: '用户名或密码错误',
            status: 500
        }
        res.send(data)
        return
    }
    // res.send( result )
    // return

    //3. 要处理成规范、格式化的json数据
    // let mid = result[0].mid
    // let username = result[0].username
    // res.send(result[0])
    // return
    let {mid,sex} = result[0]
    let user = {
        mid,//一般会根据用户的唯一标识进行加密
        username,
        sex
    }
    let token = getToken( user ) //用户名和密码验证通过，才生成 token给前端返回
    data = {
        msg:'OK',
        status:200,
        result:{
            mid,
            token,//登录成功token给前端返回，前端要把token存储在localStorage中
        }
    }


    res.send( data )
})

module.exports = router;
